package com.fengyi.library.controller;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.HashMap;
import java.util.Map;

/**
 * 服务信息控制器
 * 提供服务实例的基本信息
 */
@RestController
@RequestMapping("/api/service-info")
public class ServiceInfoController {

    @Value("${server.port}")
    private String serverPort;

    /**
     * 获取服务实例信息
     */
    @GetMapping
    public Map<String, String> getServiceInfo() {
        Map<String, String> info = new HashMap<>();
        info.put("port", serverPort);
        
        try {
            String hostname = InetAddress.getLocalHost().getHostName();
            info.put("hostname", hostname);
            info.put("instance", hostname + ":" + serverPort);
        } catch (UnknownHostException e) {
            info.put("hostname", "unknown");
            info.put("instance", "unknown:" + serverPort);
        }
        
        info.put("service", "cloud-library-service");
        info.put("timestamp", String.valueOf(System.currentTimeMillis()));

        // Console output for the instance handling this request
        System.out.println("--------------------------------------------");
        System.out.println("- ServiceInfoController on port " + serverPort + " received a request for /api/service-info -");
        System.out.println("--------------------------------------------");
        
        return info;
    }
} 